//
//  xxxViewController.m
//  shuilijuOASystem
//
//  Created by michael on 16/8/8.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "bianjianpaiViewController.h"

#import "xxxTableViewCell.h"
#import "xxxViewController.h"
#import "tablTableViewCell.h"
#import "anpaiViewController.h"
#import "GDataDefines.h"
#import "UnDoneModel.h"
#import "OADetailViewController.h"
#import "OADetailDisPatchController.h"
#import "OAUnDoneListCell.h"
#import "PickerChoiceView.h"
#import "TableViewCell.h"
#import "HeaderView.h"
@interface xxxViewController ()<UITableViewDelegate,UITableViewDataSource,TFPickerDelegate,HeaderViewDelegate,HeaderViewDelegate2>

{
    NSInteger selectIndex;
}
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,assign)int totalCounts; //总条数
@property(nonatomic,assign)int currentPage; //当前页
@property (nonatomic,strong)NSMutableArray *numArray;

@property (nonatomic,strong)NSMutableArray *mutArray;
@property (nonatomic,strong)NSMutableArray *Array;
@property (nonatomic,strong)NSMutableArray *numberArray;

@property (nonatomic,strong)NSArray *array;
@property (nonatomic,strong)NSArray *numberarray;
@property (nonatomic,strong)NSArray *weekarray;
@end

@implementation xxxViewController
-(NSMutableArray*)groupArray{

    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

-(NSMutableArray*)rowArray{
    
    if (_rowArray == nil) {
        _rowArray = [NSMutableArray array];
    }
    return _rowArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
        [self initbuttton];
//     [self requestdata];
[self loadData];
    self.view.backgroundColor = [UIColor whiteColor];
if (@available(iOS 11.0, *)) {
    self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
//    self.automaticallyAdjustsScrollViewInsets = NO;
}
       self.edgesForExtendedLayout = UIRectEdgeNone;

       _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,_button.bottom, KScreenWidth, KScreenHeight-kTopHeight) style:UITableViewStylePlain];
               self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

    _tableview.dataSource = self;
    _tableview.delegate = self;
    self.title = @"一周工作";
    [self.view addSubview:_tableview];

     UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
          UIBarButtonItem * rightIem2 = [[UIBarButtonItem alloc] initWithTitle:@"新建" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction2)];

        self.navigationItem.rightBarButtonItems = @[rightIem,rightIem2];
    // Do any additional setup after loading the view.
}
-(void)searchAction{
bianjianpaiViewController *vc = [[bianjianpaiViewController alloc]init];
[self.navigationController pushViewController:vc animated:NO];

}
-(void)searchAction2{
bianjianpaiViewController *vc = [[bianjianpaiViewController alloc]init];
vc.type = @"xinjian";
[self.navigationController pushViewController:vc animated:NO];

}
- (void)initbuttton{
  _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(0,0, KScreenWidth,40);
    [_button setTitle:@"点击此处选择领导" forState:UIControlStateNormal];
//    button .backgroundColor = [UIColor yellowColor];
    [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
      _button.enabled = NO;

    [self.view addSubview:_button];
    

}
- (void)action{
if ([_qx isEqualToString:@"no"]) {
                [self.view makeToast:@"您没有权限!"];

}else{
 PickerChoiceView *picker = [[PickerChoiceView alloc]initWithFrame:self.view.bounds];
picker.type = @"year";
    picker.delegate = self;
    
        picker.selectLb.text = @"选择领导";
        picker.customArr = _array;
    

    [self.view addSubview:picker];
}

}
- (void)PickerSelectorIndixString:(NSString *)str:(NSString *)type{

    anpaiViewController *vc = [[anpaiViewController alloc]init];
//    bianjianpaiViewController *vc = [[bianjianpaiViewController alloc]init];

  _name = str;
  
  
vc.number = _number;
vc.name = _name;
vc.array = _Array;

vc.numberArray = _numberArray;
//[self.view addSubview: vc.view];
[self.navigationController pushViewController:vc animated:YES];

    
}
- (void)requestdata{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":@"xxx"},@{@"userId":userId}];
    [HttpTools requestWithURL:kexchleader arrays:array method:@"getLeader" success:^(id JSON) {
             [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

   if(JSON ==nil){
//                       [self.view makeToast:@"您没有权限!"];
   _button.enabled = NO;
   }else{
   _button.enabled = YES;
     NSArray *array = JSON [@"data"];
          _numArray = [NSMutableArray array];
     _Array = [NSMutableArray array];
     _numberArray = [NSMutableArray array];
     for (NSDictionary *dic in array) {
    NSString *leader = dic[@"leader"];
     NSString *number = dic[@"number"];
    [_Array addObject:leader];
    [_numberArray addObject:number];
    [_numArray addObject: dic];
}
_array = _Array;
_numberarray = _numberArray;

   self.mutArray = [NSMutableArray array];

  NSArray *arr = @[@{@"title1":@"请点击此处选择类别",@"title2":_array [0]  }];
    for (NSDictionary *dict in arr) {
        
        [self.mutArray addObject:dict];
    }
_name = _Array[0];
_number = _numberarray[0];
  }
                    } failure:^(NSError *error) {
                       [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    }];

}
- (void)loadData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];

NSArray  *array = @[@{@"userId":userId}];

 [HttpTools requestWithURL:kexchleader arrays: array method:@"leaderAll" success:^(id JSON) {
 NSLog(@"%@",JSON);
         [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

     if (![JSON[@"msg"]isEqualToString:@"20"]) {
    _qx = @"no";
    self.groupArray = nil;
                    [self.view makeToast:@"您没有权限!"];

}else{
        NSArray *array = JSON [@"result"];
//      _id = JSON [@"unid"];
        for (NSDictionary *dic in array)
 {
    OANameListModel * nameLisModel = [[OANameListModel alloc]init];
        nameLisModel.subArray = [NSMutableArray array];
nameLisModel.isOpen = YES;
    nameLisModel.value = dic[@"rq"];
    NSArray *array = dic[@"data"];
    for (NSDictionary *dic in array)
    {
    OASubNameListModel *sub = [[OASubNameListModel alloc]init];

    sub.value = dic[@"leader"];
    sub.afternoon = dic[@"afternoon"];
        sub.morning = dic[@"morning"];

    [nameLisModel.subArray addObject:sub];
       [self requestdata];

    }

[_groupArray  addObject:nameLisModel];

}
}

 [MBProgressHUD hideHUDForView:self.view animated:YES];
  [self.tableview reloadData];
//     [self requestdata];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];




}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return self.groupArray.count;   
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    OANameListModel * nameListModel = self.groupArray[section];
    NSArray * tempArray = nameListModel.subArray;
    return  nameListModel.isOpen ? tempArray.count : 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];

  
    CGFloat height = 0;
    CGFloat  titlesize1  = [OAToolBox sizebyText:subModel.morning font:13 minsWidth:20];
     CGFloat  titlesize2  = [OAToolBox sizebyText:subModel.afternoon font:13 minsWidth:20];
    CGFloat  titlesize = titlesize1 +titlesize2;

  if (titlesize <  60) {
    return 100;
}
else{
 return  titlesize+40;

}
    
  
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
    OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
         static NSString *identifier=@"UITableViewCellIdentifierKey2";
    xxxTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];

if(!cell){
        cell=[[xxxTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.sub = subModel;
//    cell.textLabel.text = subModel.value;
//    cell.textLabel.font = [UIFont systemFontOfSize:13];
//    if (subModel.isSelected) {
//        cell.accessoryType = UITableViewCellAccessoryCheckmark;
//    }else{
//        cell.accessoryType = UITableViewCellAccessoryNone;
//    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    HeaderView * headView = [HeaderView headerViewWithTableView:tableView];
    headView.delegate = self;
    headView.groupModel= self.groupArray[section];
    return headView;
    
}
-(void)clickHeaderView2{
    [self.tableview reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OANameListModel * nameListModel = self.groupArray[indexPath.section];
    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
    subModel.isSelected = !subModel.isSelected;
    [self.tableview reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
